package com.oliver.oliveraiagent.agent;

import com.oliver.oliveraiagent.advisor.MyLoggerAdvisor;
import com.oliver.oliveraiagent.chatMemory.MySQLBasedChatMemory;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName OliverManus
 * @Description TODO
 * @Author snow
 * @Date 2025/5/24 1:17
 **/

/**
 * 一个具有自主决策能力的 AI 智能体
 */
//@Component
public class OliverManus extends ToolCallAgent {

    @Resource
    private MySQLBasedChatMemory mySQLBasedChatMemory;

    public OliverManus(ToolCallback[] allTools, ChatModel dashscopeChatModel,Long userId) {
        super(allTools);
        this.setName("oliverManus");
        String SYSTEM_PROMPT = """  
                You are YuManus, an all-capable AI assistant, aimed at solving any task presented by the user.  
                You have various tools at your disposal that you can call upon to efficiently complete complex requests.  
                """;
        this.setSystemPrompt(SYSTEM_PROMPT);
        String NEXT_STEP_PROMPT = """  
                Based on user needs, proactively select the most appropriate tool or combination of tools.  
                For complex tasks, you can break down the problem and use different tools step by step to solve it.  
                After using each tool, clearly explain the execution results and suggest the next steps.  
                If you want to stop the interaction at any point, use the `terminate` tool/function call.  
                """;
        this.setNextStepPrompt(NEXT_STEP_PROMPT);
        this.setMaxSteps(20);
        // 初始化客户端
        ChatMemory chatMemory = mySQLBasedChatMemory;
        ChatClient chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultAdvisors(
                        //  会话记忆前置监控器
                        new MessageChatMemoryAdvisor(chatMemory),
                        //  自定义日志
                        new MyLoggerAdvisor()
                )
                .defaultToolContext(Map.of("userId",userId))
                .build();

        this.setChatClient(chatClient);
    }
}

